package com.share.device.controller;

import com.share.common.core.web.controller.BaseController;
import com.share.common.core.web.domain.AjaxResult;
import com.share.common.core.web.page.TableDataInfo;
import com.share.common.log.annotation.Log;
import com.share.common.log.enums.BusinessType;
import com.share.common.security.annotation.RequiresPermissions;
import com.share.device.domain.CabinetType;
import com.share.device.service.ICabinetTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@Tag(name = "柜机类型接口管理")
@RestController
@RequestMapping("/cabinetType")
@RequiredArgsConstructor
@Slf4j
public class CabinetTypeController extends BaseController {
    private final ICabinetTypeService cabinetTypeService;

    @Operation(summary = "查询全部柜机类型列表")
    @GetMapping("/getCabinetTypeList")
    public AjaxResult getCabinetTypeList() {
        return success(cabinetTypeService.list());
    }

    /**
     * 删除柜机类型
     * 根据id删除某个数据，或者根据多个id删除多条记录
     * [1,2,3]
     */
    @Operation(summary = "删除柜机类型")
    @RequiresPermissions("device:cabinetType:remove")
    @Log(title = "柜机类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult delete(@PathVariable Long[] ids) {
        boolean is_Success = cabinetTypeService.removeBatchByIds(Arrays.asList(ids));
        return toAjax(is_Success);
    }

    /**
     * 修改柜机类型
     */
    @Operation(summary = "修改柜机类型")
    @RequiresPermissions("device:cabinetType:edit")
    @Log(title = "柜机类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult update(@RequestBody @Validated CabinetType cabinetType) {
        boolean is_Success = cabinetTypeService.updateById(cabinetType);
        return toAjax(is_Success);
    }

    /**
     * 新增柜机类型
     */
    @Operation(summary = "新增柜机类型")
    @RequiresPermissions("device:cabinetType:add")
    @Log(title = "柜机类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated CabinetType cabinetType) {
        boolean is_Success = cabinetTypeService.save(cabinetType);
        return toAjax(is_Success);
    }

    /**
     * 获取柜机类型详细信息
     */
    @Operation(summary = "根据id查询详情")
    @GetMapping("/{id}")
    @RequiresPermissions("device:cabinetType:query")
    public AjaxResult getCabinetType(@PathVariable Long id) {
        CabinetType cabinetType = cabinetTypeService.getById(id);
        return success(cabinetType);
    }

    /**
     * 查询柜机类型列表
     *
     * @param cabinetType 柜机类型
     * @return 柜机类型集合
     */
    @Operation(summary = "柜机类型分页查询")
    @RequiresPermissions("device:cabinetType:list")
    @Log(title = "柜机类型查询", businessType = BusinessType.OTHER)
    @GetMapping("/list")
    public TableDataInfo list(CabinetType cabinetType) {
        //封装分页参数数据
        startPage();
        //调用service查询数据库
        List<CabinetType> list = cabinetTypeService.selectCabinetTypeList(cabinetType);
        return getDataTable(list);
    }

    /**
     * 自定义验证异常
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Object handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        log.error(e.getMessage(), e);
        String message = Objects.requireNonNull(e.getBindingResult().getFieldError()).getDefaultMessage();
        return AjaxResult.error(message);
    }

}
